package com.nowcoder.community.service;

import com.nowcoder.community.entity.DiscussPost;
import com.nowcoder.community.entity.User;
import com.nowcoder.community.repository.AlphaRepository;
import com.nowcoder.community.repository.DiscussPostMapper;
import com.nowcoder.community.repository.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.Date;

@Service
@Scope("prototype")
public class AlphaService {
    public static final String name = AlphaService.class.getName();

    @Autowired
    private AlphaRepository alphaRepository;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DiscussPostMapper discussPostMapper;

    @Autowired
    private TransactionTemplate transactionTemplate;

    public AlphaService() {
        System.out.println("实例化 " + name);
    }

    @PostConstruct
    public void init(){
        System.out.println("初始化 " + name);
    }

    @PreDestroy
    public void destory(){
        System.out.println("销毁 " + name);
    }

    public String find(){
        return alphaRepository.select();
    }


    // 新人报道：添加新用户并发布帖子
    // 声明式事务
    @Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
    public void newUserReport1(){
        User user = new User();
        user.setUsername("user1");
        user.setPassword("password");
        user.setSalt("salt");
        user.setHeaderUrl("header.png");
        user.setEmail("user1@qq.com");
        user.setActivationCode("code");
        user.setCreateTime(new Date());
        userMapper.insertUser(user);

        DiscussPost post = new DiscussPost();
        post.setUserId(user.getId());
        post.setTitle("title1");
        post.setContent("content1");
        post.setCreateTime(new Date());
        discussPostMapper.insertDiscussPost(post);

        throw new RuntimeException("error");
    }

    // 编程式事务
    public Object newUserReport2(){

        transactionTemplate.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
        transactionTemplate.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);

        return transactionTemplate.execute(new TransactionCallback<Object>() {
            @Override
            public Object doInTransaction(TransactionStatus transactionStatus) {
                User user = new User();
                user.setUsername("user2");
                user.setPassword("password");
                user.setSalt("salt");
                user.setHeaderUrl("header.png");
                user.setEmail("user2@qq.com");
                user.setActivationCode("code");
                user.setCreateTime(new Date());
                userMapper.insertUser(user);

                DiscussPost post = new DiscussPost();
                post.setUserId(user.getId());
                post.setTitle("title2");
                post.setContent("content2");
                post.setCreateTime(new Date());
                discussPostMapper.insertDiscussPost(post);

                return Integer.valueOf("abc");
            }
        });
    }
}
